package com.fsy.beanandsql.beantosql;

import com.fsy.beanandsql.beantosql.Bean.TestBean;
import com.fsy.beanandsql.beantosql.Bean.TestChildBean;
import com.fsy.beanandsql.beantosql.Bean.TestChildsBean;

import javax.persistence.Entity;

/**
 * Created by shuangyufeng on 2017/5/5.
 */
public class TestGetTableName {

    public static void main(String[] args){
        //普通类获取类名称
        TestBean testBean = new TestBean();
        getTableName(testBean.getClass());

        System.out.println("<<<<<<<<===========分割线======================>>>>>>>");
        //继承类获取父类的名称
        TestChildBean testChildBean = new TestChildBean();
        getTableName(testChildBean.getClass());

        System.out.println("<<<<<<<<===========分割线======================>>>>>>>");
        //继承类获取父类的名称 再加一层试试
        TestChildsBean testChildsBean = new TestChildsBean();
        getTableName(testChildsBean.getClass());

    }

    private static void getTableName(Class<?> objClass){
        Entity entity = getEntityClass(objClass);
        System.out.println("Entity name == " + entity.name());
    }


    private static Entity getEntityClass(Class<?> objClass){
        Entity entity = objClass.getAnnotation(Entity.class);
        if(entity == null){
            return getEntityClass(objClass.getSuperclass());
        }
        return entity;
    }
}
